#include "frmObjects.h"
#include "GameSystemManager.h"
#include "GameObjectManager.h"
//#include "Physics.h"
#include "GUISystem.h"
#include "TerrainSystem.h"
#include "TextOutput.h"

#include "CPosition.h"
#include "COrientation.h"
#include "CMesh.h"
#include "CRender.h"
#include "CLight.h"
#include "CPhysics.h"
#include "Physics.h"

#include <sstream>

frmObjects::frmObjects() {
	subscribeEvents();
	setup();
}
frmObjects::~frmObjects() {

}

void frmObjects::subscribeEvents() {

	////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//Subsribe GUI Objects events

	CEGUI::Window* lstObjectList = CEGUI::WindowManager::getSingleton().getWindow("Objects/lstObjects");
	lstObjectList->subscribeEvent(CEGUI::Listbox::EventSelectionChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsList_click,this));

	CEGUI::Window* lstObjectMeshList = CEGUI::WindowManager::getSingleton().getWindow("Objects/lstMesh");
	lstObjectMeshList->subscribeEvent(CEGUI::Listbox::EventSelectionChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsListMesh_click,this));

	CEGUI::Window* btnObjectsNew = CEGUI::WindowManager::getSingleton().getWindow("Objects/btnCreateObject");
	btnObjectsNew->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmObjects::evtObjectsNew_click,this));

	CEGUI::Window* btnObjectsDelete = CEGUI::WindowManager::getSingleton().getWindow("Objects/btnDeleteObject");
	btnObjectsDelete->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmObjects::evtObjectsDelete_click,this));

	CEGUI::Window* btnObjectsSave = CEGUI::WindowManager::getSingleton().getWindow("Objects/btnSaveObject");
	btnObjectsSave->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmObjects::evtObjectsSave_click,this));

	CEGUI::Window* btnObjectsSpawn = CEGUI::WindowManager::getSingleton().getWindow("Objects/btnSpawn");
	btnObjectsSpawn->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmObjects::evtObjectsSpawn_click,this));

	CEGUI::Window* btnObjectsResetRot = CEGUI::WindowManager::getSingleton().getWindow("Objects/btnResetRotation");
	btnObjectsResetRot->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmObjects::evtObjectsResetRotation_click,this));

	CEGUI::RadioButton* optMeshObj = (CEGUI::RadioButton*) CEGUI::WindowManager::getSingleton().getWindow("Objects/optMeshObj");
	optMeshObj->subscribeEvent(CEGUI::RadioButton::EventSelectStateChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsMeshOpt_change,this));

	CEGUI::RadioButton* optLightObj = (CEGUI::RadioButton*) CEGUI::WindowManager::getSingleton().getWindow("Objects/optLightObj");
	optLightObj->subscribeEvent(CEGUI::RadioButton::EventSelectStateChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightOpt_change,this));

	CEGUI::Checkbox* optLightChkIsRagdoll = (CEGUI::Checkbox*) CEGUI::WindowManager::getSingleton().getWindow("Objects/chkIsRagdoll");
	optLightChkIsRagdoll->subscribeEvent(CEGUI::RadioButton::EventSelectStateChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightIsRagdoll_change,this));

	CEGUI::Combobox* cmbLightType = (CEGUI::Combobox*) CEGUI::WindowManager::getSingleton().getWindow("Objects/cmbLightType");
	cmbLightType->subscribeEvent(CEGUI::Combobox::EventListSelectionAccepted,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightType_change,this));

	CEGUI::Editbox* txtLightType = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightRange");
	txtLightType->subscribeEvent(CEGUI::Editbox::EventTextChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightRange_change,this));

	CEGUI::Editbox* txtLightConstant = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightConstant");
	txtLightConstant->subscribeEvent(CEGUI::Editbox::EventTextChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightConstant_change,this));

	CEGUI::Editbox* txtLightLinear = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightLinear");
	txtLightLinear->subscribeEvent(CEGUI::Editbox::EventTextChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightLinear_change,this));

	CEGUI::Editbox* txtLightQuadratic = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightCuadratic");
	txtLightQuadratic->subscribeEvent(CEGUI::Editbox::EventTextChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightQuadratic_change,this));

	CEGUI::Window* sldLightInner = CEGUI::WindowManager::getSingleton().getWindow("Objects/sldInnerAngle");
	sldLightInner->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightInner_change,this));
	
	CEGUI::Window* sldLightOuter = CEGUI::WindowManager::getSingleton().getWindow("Objects/sldOuterAngle");
	sldLightOuter->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightOuter_change,this));

	CEGUI::Window* sldLightDiffuseRed = CEGUI::WindowManager::getSingleton().getWindow("Objects/sldDiffurseRed");
	sldLightDiffuseRed->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightDiffuseRed_change,this));

	CEGUI::Window* sldLightDiffuseGreen = CEGUI::WindowManager::getSingleton().getWindow("Objects/sldDiffurseGreen");
	sldLightDiffuseGreen->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightDiffuseGreen_change,this));

	CEGUI::Window* sldLightDiffuseBlue = CEGUI::WindowManager::getSingleton().getWindow("Objects/sldDiffurseBlue");
	sldLightDiffuseBlue->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightDiffuseBlue_change,this));

	CEGUI::Window* sldLightSpecularRed = CEGUI::WindowManager::getSingleton().getWindow("Objects/sldSpecularRed");
	sldLightSpecularRed->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightSpecularRed_change,this));

	CEGUI::Window* sldLightSpecularGreen = CEGUI::WindowManager::getSingleton().getWindow("Objects/sldSpecularGreen");
	sldLightSpecularGreen->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightSpecularGreen_change,this));

	CEGUI::Window* sldLightSpecularBlue = CEGUI::WindowManager::getSingleton().getWindow("Objects/sldSpecularBlue");
	sldLightSpecularBlue->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightSpecularBlue_change,this));

	CEGUI::Window* btnObjectsFollowCursor = CEGUI::WindowManager::getSingleton().getWindow("Objects/btnFollowCursor");
	btnObjectsFollowCursor->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmObjects::evtObjectsFollowCursor_click,this));
	
	CEGUI::Window* wndObjectsClose = CEGUI::WindowManager::getSingleton().getWindow("Objects");
	wndObjectsClose->subscribeEvent(CEGUI::FrameWindow::EventCloseClicked,CEGUI::Event::Subscriber(&frmObjects::evtObjectsClose_click,this));

}

void frmObjects::setup() {
	
	isLoading = false;

	CEGUI::Window* w = CEGUI::WindowManager::getSingleton().getWindow("Objects");
	w->setVisible(true);

	CEGUI::Combobox* cmb = (CEGUI::Combobox*)CEGUI::WindowManager::getSingleton().getWindow("Objects/cmbLightType");
	cmb->setReadOnly(true);

	const CEGUI::Image* sel_img = &CEGUI::ImagesetManager::getSingleton().getImageset("TaharezLook")->getImage("MultiListSelectionBrush");
	CEGUI::ListboxTextItem* itm1;
	itm1 = new CEGUI::ListboxTextItem("LT_DIRECTIONAL", 1);
    itm1->setSelectionBrushImage(sel_img);
    cmb->addItem(itm1);
	
	CEGUI::ListboxTextItem* itm2;
	itm2 = new CEGUI::ListboxTextItem("LT_POINT", 2);
    itm2->setSelectionBrushImage(sel_img);
    cmb->addItem(itm2);

	CEGUI::ListboxTextItem* itm3;
	itm3 = new CEGUI::ListboxTextItem("LT_SPOTLIGHT", 3);
    itm3->setSelectionBrushImage(sel_img);
    cmb->addItem(itm3);

	CEGUI::Slider* sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Objects/sldInnerAngle");
	sld->setMaxValue(180);
	sld->setCurrentValue(0);
	//CEGUI::Window *lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Objects/lblYaw");
	//lbl->setText("0");

	sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Objects/sldOuterAngle");
	sld->setMaxValue(180);
	sld->setCurrentValue(0);

	sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Objects/sldDiffurseRed");
	sld->setMaxValue(1);
	sld->setCurrentValue(0);

	sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Objects/sldDiffurseGreen");
	sld->setMaxValue(1);
	sld->setCurrentValue(0);

	sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Objects/sldDiffurseBlue");
	sld->setMaxValue(1);
	sld->setCurrentValue(0);

	sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Objects/sldSpecularRed");
	sld->setMaxValue(1);
	sld->setCurrentValue(0);

	sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Objects/sldSpecularGreen");
	sld->setMaxValue(1);
	sld->setCurrentValue(0);

	sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Objects/sldSpecularBlue");
	sld->setMaxValue(1);
	sld->setCurrentValue(0);

	//lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Objects/lblPitch");
	//lbl->setText("0");

	//sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Objects/sldRoll");
	//sld->setMaxValue(360);
	//sld->setCurrentValue(0);
	//lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Objects/lblRoll");
	//lbl->setText("0");

	fillMeshList();
	fillObjectsList();
}

void frmObjects::fillObjectsList() {
	CEGUI::Listbox* lbox = (CEGUI::Listbox*) CEGUI::WindowManager::getSingleton().getWindow("Objects/lstObjects");
	lbox->resetList();

	std::map<const std::string, GameObject*> gos;
	gos = GameObjectManager::getSingleton()->getGOs();

	std::map<const std::string, GameObject*>::iterator iter;
	for(iter = gos.begin(); iter != gos.end(); iter++) {
		CEGUI::ListboxTextItem* item = new CEGUI::ListboxTextItem(iter->first);
		lbox->addItem(item);
	}
}

void frmObjects::fillMeshList() {

	CEGUI::Listbox* lbox = (CEGUI::Listbox*) CEGUI::WindowManager::getSingleton().getWindow("Objects/lstMesh");
	lbox->resetList();
	StringVectorPtr resourceNames = ResourceGroupManager::getSingleton().findResourceNames("General","*.mesh" );
	std::vector<Ogre::String>::const_iterator itResourceName = resourceNames->begin();
	while ( itResourceName != resourceNames->end() )
	{
		CEGUI::ListboxTextItem* item = new CEGUI::ListboxTextItem(*itResourceName);
		lbox->addItem(item);
		++itResourceName;
	} 

}

void frmObjects::addNewGO() {

	std::string objName;

	bool isMesh;
	bool isLight;
	bool isRagdoll;

	std::string meshFileName;
	std::string lightType;

	isRagdoll = false;
	isMesh = false;
	isLight = false;

	CEGUI::Editbox* txtName = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Objects/txtName");
	if(txtName->getText() != "") // && gom_config.selectedMesh != ""
	{
		objName = txtName->getText().c_str();

		CEGUI::RadioButton* optMeshObj = (CEGUI::RadioButton*) CEGUI::WindowManager::getSingleton().getWindow("Objects/optMeshObj");
		if(optMeshObj->isSelected()) {
			if(gom_config.selectedMesh != "")
				isMesh = true;
			else
				return;
		}
		CEGUI::RadioButton* optLightObj = (CEGUI::RadioButton*) CEGUI::WindowManager::getSingleton().getWindow("Objects/optLightObj");
		if(optLightObj->isSelected()) {
			isLight = true;
		}
		
		isRagdoll = GameSystemManager::getSingleton()->getGUI()->CheckBoxToBool("Objects/chkIsRagdoll");

		//if(isRagdoll) {
		//	GameObjectManager::getSingleton()->addCustomGO(objName,isMesh,isLight,isRagdoll,gom_config.selectedMesh,1,1,1,600,300,280);
		//} else {
			GameObjectManager::getSingleton()->addCustomGO(objName,isMesh,isLight,isRagdoll,gom_config.selectedMesh);
			gom_config.objectInHand = objName;
			gom_config.isHoldingItem = true;
		//}

		fillObjectsList();
	}
}
void frmObjects::deleteGO() {
	
	if(gom_config.objectSelected=="")
		return;

	CPhysics *physics = dynamic_cast<CPhysics*>(GameObjectManager::getSingleton()->getGO(gom_config.objectSelected)->getComponent("CPhysics"));
	if(physics) {
		//int id = physics->id;
		//deleteActor(gom_config.objectSelected);
	}

	GameObjectManager::getSingleton()->deleteGO(gom_config.objectSelected);

	gom_config.isHoldingItem = false;
	gom_config.objectInHand = "";
	
	gom_config.isObjectSelected = false;
	gom_config.objectSelected = "";
	
	GameObjectManager::getSingleton()->setVisibleObjectDecal(false);

	fillObjectsList();
}
void frmObjects::saveGO() {
	
	fillObjectsList();
}
void frmObjects::spawnGO() {
	
}

void frmObjects::fillConfigFromGui() {
}
void frmObjects::updateControls() {
    
	if(GameSceneManager::getSingleton()->getBrush() == BRUSH_OBJECT)	{
		if(gom_config.isObjectSelected) {
			//if(GameSystemManager::getSingleton()->getInput()->mouseLeftButton)	{
				if(gom_config.objectSelected != "") {
					CEGUI::Listbox* list = static_cast<CEGUI::Listbox*> (CEGUI::WindowManager::getSingleton().getWindow("Objects/lstObjects"));
					const CEGUI::String Name = list->getName ();
					
					CEGUI::ListboxTextItem* item =  (CEGUI::ListboxTextItem*) list->findItemWithText(gom_config.objectSelected,0); // getFirstSelectedItem ();
					list->setItemSelectState(item,true);
					item->setSelected(true);
					item->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
					fillLightDataFromObject(gom_config.objectSelected);

				}
		}
	}

/*
	CEGUI::WindowManager& wMgr = CEGUI::WindowManager::getSingleton();
	CEGUI::Listbox* matList  =  static_cast<CEGUI::Listbox*> (CEGUI::WindowManager::getSingleton().getWindow("Objects/lstObjects"));
	CEGUI::ListboxTextItem* itemMat = (CEGUI::ListboxTextItem*) matList->findItemWithText(matName, 0);
	matList->setItemSelectState (itemMat, true);
	
	gom_config.objectInHand = matName;
	gom_config.isHoldingItem = true;
*/

}

//////////////////////////////////////////////////////////////////////////////////////////////////////
bool frmObjects::evtObjectsClose_click(const CEGUI::EventArgs& e) {
	CEGUI::Window* w = CEGUI::WindowManager::getSingleton().getWindow("Objects");
	w->setVisible(false);
	return true;
}
bool frmObjects::evtObjectsList_click(const CEGUI::EventArgs& e) {
	if(GameSceneManager::getSingleton()->getBrush() == BRUSH_OBJECT)	{

		const CEGUI::WindowEventArgs& wEventArgs = static_cast<const CEGUI::WindowEventArgs&>(e);
		CEGUI::Listbox* list = static_cast<CEGUI::Listbox*>(wEventArgs.window);
		const CEGUI::String Name = list->getName ();
		size_t selCount = list->getSelectedCount ();
		if (selCount == 1)
		{
			CEGUI::ListboxTextItem* item =  (CEGUI::ListboxTextItem*) list->getFirstSelectedItem ();
			const unsigned int pos = StringConverter::parseUnsignedInt (item->getText().c_str ());   
			const char *matName = item->getText().c_str();;
			item->setSelected(true);
			item->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");

			CEGUI::WindowManager& wMgr = CEGUI::WindowManager::getSingleton();
			CEGUI::Listbox* matList  =  static_cast<CEGUI::Listbox*> (CEGUI::WindowManager::getSingleton().getWindow("Objects/lstObjects"));
			CEGUI::ListboxTextItem* itemMat = (CEGUI::ListboxTextItem*) matList->findItemWithText(matName, 0);
			matList->setItemSelectState (itemMat, true);
			
			//gom_config.objectInHand = matName;
			//gom_config.isHoldingItem = true;

			if(gom_config.objectSelected != "") {
				CRender *prev_mov = dynamic_cast<CRender*>(GameObjectManager::getSingleton()->getGO(gom_config.objectSelected)->getComponent("CRender"));
				if(prev_mov) {
					GameObjectManager::getSingleton()->setVisibleObjectDecal(false);
					prev_mov->mNode->showBoundingBox(false);
				}
			}

			gom_config.isObjectSelected = true;
			gom_config.objectSelected = matName;

			CRender *mov = dynamic_cast<CRender*>(GameObjectManager::getSingleton()->getGO(gom_config.objectSelected)->getComponent("CRender"));
			if(mov) {
				mov->mNode->showBoundingBox(true);
				GameObjectManager::getSingleton()->setVisibleObjectDecal(true);
				GameObjectManager::getSingleton()->updateObjectDecalPosition(mov->mNode->getPosition());

				updateControls();
			}

			

		}
	}
	return true;
}
bool frmObjects::evtObjectsListMesh_click(const CEGUI::EventArgs& e) {
    const CEGUI::WindowEventArgs& wEventArgs = static_cast<const CEGUI::WindowEventArgs&>(e);
    CEGUI::Listbox* list = static_cast<CEGUI::Listbox*>(wEventArgs.window);
    const CEGUI::String Name = list->getName ();
    size_t selCount = list->getSelectedCount ();
    if (selCount == 1)
    {
        CEGUI::ListboxTextItem* item =  (CEGUI::ListboxTextItem*) list->getFirstSelectedItem ();
        const unsigned int pos = StringConverter::parseUnsignedInt (item->getText().c_str ());   

        {
			const char *matName = item->getText().c_str();;
			item->setSelected(true);
			item->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");

            CEGUI::WindowManager& wMgr = CEGUI::WindowManager::getSingleton();
            CEGUI::Listbox* matList  =  static_cast<CEGUI::Listbox*> (CEGUI::WindowManager::getSingleton().getWindow("Objects/lstMesh"));
            CEGUI::ListboxTextItem* itemMat = (CEGUI::ListboxTextItem*) matList->findItemWithText(matName, 0);
            matList->setItemSelectState (itemMat, true);
			
			gom_config.selectedMesh = matName;
        }
	}
	return true;
}
bool frmObjects::evtObjectsNew_click(const CEGUI::EventArgs& e) {
	addNewGO();
	return true;
}
bool frmObjects::evtObjectsDelete_click(const CEGUI::EventArgs& e) {
	deleteGO();
	return true;
}
bool frmObjects::evtObjectsSave_click(const CEGUI::EventArgs& e) {
	saveGO();
	return true;
}
bool frmObjects::evtObjectsSpawn_click(const CEGUI::EventArgs& e) {
	//spawnGO();
	
	return true;
}
bool frmObjects::evtObjectsResetRotation_click(const CEGUI::EventArgs& e) {
	if(gom_config.objectSelected != "") {
		CRender *prev_mov = dynamic_cast<CRender*>( (GameObjectManager::getSingleton()->getGO(gom_config.objectSelected))->getComponent("CRender") );
		if(prev_mov) {
			prev_mov->mNode->setOrientation(Quaternion::IDENTITY);
		}
	}
	return true;
}


/*
bool frmObjects::evtObjectsYaw_change(const CEGUI::EventArgs& e) {
	

	return true;
}

bool frmObjects::evtObjectsPitch_change(const CEGUI::EventArgs& e) {
	CEGUI::Slider* sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Objects/sldPitch");

	sld->getCurrentValue();

	if(gom_config.isObjectSelected) {
		CMovable *cm = dynamic_cast<CMovable*>((GameObjectManager::getSingleton()->getGO(gom_config.objectSelected))->getComponent("CMovable"));
		if(cm) {
			Quaternion origin = cm->mNode->getOrientation();
			cm->mNode->rotate(Vector3::UNIT_Z,(Ogre::Radian)(Ogre::Math::AngleUnitsToRadians(sld->getCurrentValue())));
		}
	}

	std::ostringstream buffer;
	buffer << sld->getCurrentValue();

	CEGUI::Window *lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Objects/lblPitch");
	lbl->setText( static_cast<CEGUI::String>(buffer.str()) );

	return true;
}
bool frmObjects::evtObjectsRoll_change(const CEGUI::EventArgs& e) {
	CEGUI::Slider* sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Objects/sldRoll");

	sld->getCurrentValue();

	if(gom_config.isObjectSelected) {
		CMovable *cm = dynamic_cast<CMovable*>((GameObjectManager::getSingleton()->getGO(gom_config.objectSelected))->getComponent("CMovable"));
		if(cm) {
			Quaternion origin = cm->mNode->getOrientation();
			//origin.FromAngleAxis((Ogre::Radian)(Ogre::Math::AngleUnitsToRadians(sld->getCurrentValue())),Vector3::UNIT_X);
			cm->mNode->rotate(Vector3::UNIT_X,(Ogre::Radian)(Ogre::Math::AngleUnitsToRadians(sld->getCurrentValue())));
			//Quaternion q = Quaternion(Degree(sld->getCurrentValue()), Vector3::UNIT_Z);
			//cm->mNode->setOrientation(origin);
		}
	}

	std::ostringstream buffer;
	buffer << sld->getCurrentValue();

	CEGUI::Window *lbl = (CEGUI::Window*)CEGUI::WindowManager::getSingleton().getWindow("Objects/lblRoll");
	lbl->setText( static_cast<CEGUI::String>(buffer.str()) );

	return true;
}
*/

void frmObjects::fillLightDataFromObject(std::string object) {
	
	isLoading = true;
	
	CMesh *objMesh = dynamic_cast<CMesh*>(GameObjectManager::getSingleton()->getGO(object)->getComponent("CMesh"));
	if(objMesh) {
		CEGUI::Listbox* listMesh = static_cast<CEGUI::Listbox*> (CEGUI::WindowManager::getSingleton().getWindow("Objects/lstMesh"));
		CEGUI::ListboxTextItem* itemMesh =  (CEGUI::ListboxTextItem*) listMesh->findItemWithText(objMesh->meshFileName,0); // getFirstSelectedItem ();
		listMesh->setItemSelectState(itemMesh,true);
		itemMesh->setSelected(true);
		itemMesh->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");

		CEGUI::RadioButton* rad = (CEGUI::RadioButton*) (CEGUI::WindowManager::getSingleton().getWindow("Objects/optMeshObj"));
		rad->setSelected(true);
	}

	CLight *objLight = dynamic_cast<CLight*>(GameObjectManager::getSingleton()->getGO(object)->getComponent("CLight"));
	if(objLight) {

		CEGUI::RadioButton* rad = (CEGUI::RadioButton*) (CEGUI::WindowManager::getSingleton().getWindow("Objects/optLightObj"));
		rad->setSelected(true);

		//CEGUI::Listbox* listMesh = static_cast<CEGUI::Listbox*> (CEGUI::WindowManager::getSingleton().getWindow("Objects/lstMesh"));
		//CEGUI::ListboxTextItem* itemMesh =  (CEGUI::ListboxTextItem*) listMesh->findItemWithText(objLight->meshFileName,0); // getFirstSelectedItem ();
		//listMesh->setItemSelectState(itemMesh,true);
		//itemMesh->setSelected(true);
		//itemMesh->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
		
		
		CEGUI::Combobox* cmb = (CEGUI::Combobox*)CEGUI::WindowManager::getSingleton().getWindow("Objects/cmbLightType");

		
		std::string light_type_name; //= (std::string)cmb->getText().c_str();
		//unsigned int fog_type;
		Ogre::Light::LightTypes light_type = objLight->mLight->getType();
		
		if(light_type == Ogre::Light::LT_DIRECTIONAL) {
			light_type_name = "LT_DIRECTIONAL";
		} else if (light_type == Ogre::Light::LT_POINT) {
			light_type_name = "LT_POINT";
		} else if(light_type == Ogre::Light::LT_SPOTLIGHT) {
			light_type_name = "LT_SPOTLIGHT";
		} else {
			light_type_name = "LT_POINT";
		}
		
		CEGUI::ListboxTextItem* itemLight =  (CEGUI::ListboxTextItem*) cmb->findItemWithText(light_type_name,0);
		cmb->setItemSelectState(itemLight,true);
		cmb->requestRedraw();
		cmb->setText(light_type_name);
		itemLight->setSelected(true);
		itemLight->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");
		
		CEGUI::Editbox* txtRange = (CEGUI::Editbox*)CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightRange");
		txtRange->disable();
		//Ogre::Real range = objLight->mLight->getAttenuationRange();
		txtRange->setText(StringConverter::toString(objLight->mLight->getAttenuationRange()));
		txtRange->enable();

		CEGUI::Editbox* txtConstant = (CEGUI::Editbox*)CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightConstant");
		//Ogre::Real constant = atof(txtConstant->getText().c_str());
		txtConstant->setText(StringConverter::toString(objLight->mLight->getAttenuationConstant()));

		CEGUI::Editbox* txtLinear = (CEGUI::Editbox*)CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightLinear");
		//Ogre::Real linear = atof(txtLinear->getText().c_str());
		txtLinear->setText(StringConverter::toString(objLight->mLight->getAttenuationLinear()));

		CEGUI::Editbox* txtQuadratic = (CEGUI::Editbox*)CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightCuadratic");
		//Ogre::Real quadratic = atof(txtQuadratic->getText().c_str());
		txtQuadratic->setText(StringConverter::toString(objLight->mLight->getAttenuationQuadric()));

		CEGUI::Slider* sld = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldInnerAngle");
		Ogre::Real inner = objLight->mLight->getSpotlightInnerAngle().valueDegrees();
		sld->setCurrentValue(inner);

		sld = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldOuterAngle");
		Ogre::Real outer = objLight->mLight->getSpotlightOuterAngle().valueDegrees();
		sld->setCurrentValue(outer);

		CEGUI::Slider* sldDifRed = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldDiffurseRed");
		CEGUI::Slider* sldDifGreen = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldDiffurseGreen");
		CEGUI::Slider* sldDifBlue = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldDiffurseBlue");

		Ogre::ColourValue difColour = objLight->mLight->getDiffuseColour();
		
		sldDifRed->setCurrentValue(difColour.r);
		sldDifGreen->setCurrentValue(difColour.g);
		sldDifBlue->setCurrentValue(difColour.b);
		
		CEGUI::Slider* sldSpecRed = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldSpecularRed");
		CEGUI::Slider* sldSpecGreen = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldSpecularGreen");
		CEGUI::Slider* sldSpecBlue = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldSpecularBlue");

		Ogre::ColourValue specColour = objLight->mLight->getSpecularColour();
		
		sldSpecRed->setCurrentValue(specColour.r);
		sldSpecGreen->setCurrentValue(specColour.g);
		sldSpecBlue->setCurrentValue(specColour.b);

	}
	
	isLoading = false;
}

void frmObjects::updateLight() {
	
	CEGUI::RadioButton* optLightObj = (CEGUI::RadioButton*) CEGUI::WindowManager::getSingleton().getWindow("Objects/optLightObj");
	if(optLightObj->isSelected()) {
		CEGUI::Combobox* cmb = (CEGUI::Combobox*)CEGUI::WindowManager::getSingleton().getWindow("Objects/cmbLightType");

		std::string light_type_name = (std::string)cmb->getText().c_str();
		//unsigned int fog_type;
		Ogre::Light::LightTypes light_type;
		
		if(light_type_name == "LT_DIRECTIONAL") {
			light_type = Ogre::Light::LT_DIRECTIONAL;
		} else if (light_type_name == "LT_POINT") {
			light_type = Ogre::Light::LT_POINT;
		} else if(light_type_name == "LT_SPOTLIGHT") {
			light_type = Ogre::Light::LT_SPOTLIGHT;
		} else {
			light_type = Ogre::Light::LT_POINT;
		}

		CEGUI::Editbox* txtRange = (CEGUI::Editbox*)CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightRange");
		Ogre::Real range = atof(txtRange->getText().c_str());

		CEGUI::Editbox* txtConstant = (CEGUI::Editbox*)CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightConstant");
		Ogre::Real constant = atof(txtConstant->getText().c_str());

		CEGUI::Editbox* txtLinear = (CEGUI::Editbox*)CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightLinear");
		Ogre::Real linear = atof(txtLinear->getText().c_str());

		CEGUI::Editbox* txtQuadratic = (CEGUI::Editbox*)CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightCuadratic");
		Ogre::Real quadratic = atof(txtQuadratic->getText().c_str());


		CEGUI::Slider* sld = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldInnerAngle");
		Ogre::Radian inner = (Ogre::Radian)Ogre::Math::AngleUnitsToRadians(sld->getCurrentValue());
		
		sld = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldOuterAngle");
		Ogre::Radian outer = (Ogre::Radian)Ogre::Math::AngleUnitsToRadians(sld->getCurrentValue());

		sld = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldDiffurseRed");
		Ogre::ColourValue difColour;
		difColour.r = sld->getCurrentValue();

		sld = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldDiffurseGreen");
		difColour.g = sld->getCurrentValue();

		sld = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldDiffurseBlue");
		difColour.b = sld->getCurrentValue();

		sld = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldSpecularRed");
		Ogre::ColourValue specColour;
		specColour.r = sld->getCurrentValue();

		sld = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldSpecularGreen");
		specColour.g = sld->getCurrentValue();

		sld = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Objects/sldSpecularBlue");
		specColour.b = sld->getCurrentValue();

		if(gom_config.objectSelected != "") {
			CLight *light = dynamic_cast<CLight*>( (GameObjectManager::getSingleton()->getGO(gom_config.objectSelected))->getComponent("CLight") );
			if(light) {
				light->mLight->setType(light_type);
				light->mLight->setAttenuation(range,constant,linear,quadratic);
				
				if(light_type == Ogre::Light::LT_SPOTLIGHT)
					light->mLight->setSpotlightRange(inner,outer);
				
				light->mLight->setDiffuseColour(difColour);
				light->mLight->setSpecularColour(specColour);
			}
		}


	}
	
}

bool frmObjects::evtObjectsMeshOpt_change(const CEGUI::EventArgs& e) {
	
	return true;
}
bool frmObjects::evtObjectsLightOpt_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}
bool frmObjects::evtObjectsLightIsRagdoll_change(const CEGUI::EventArgs& e) {
	//if(!isLoading)
	//	updateLight();
	return true;
}
bool frmObjects::evtObjectsLightType_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}
bool frmObjects::evtObjectsLightRange_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}
bool frmObjects::evtObjectsLightConstant_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}
bool frmObjects::evtObjectsLightLinear_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}
bool frmObjects::evtObjectsLightQuadratic_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}
bool frmObjects::evtObjectsLightInner_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}
bool frmObjects::evtObjectsLightOuter_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}

bool frmObjects::evtObjectsLightDiffuseRed_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}
bool frmObjects::evtObjectsLightDiffuseGreen_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}
bool frmObjects::evtObjectsLightDiffuseBlue_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}

bool frmObjects::evtObjectsLightSpecularRed_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}
bool frmObjects::evtObjectsLightSpecularGreen_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}
bool frmObjects::evtObjectsLightSpecularBlue_change(const CEGUI::EventArgs& e) {
	if(!isLoading)
		updateLight();
	return true;
}
bool frmObjects::evtObjectsFollowCursor_click(const CEGUI::EventArgs& e) {
	//displayDegugInfo();
	/*
	if(GameSceneManager::getSingleton()->getBrush() == BrushType::BRUSH_OBJECT)	{
		if(gom_config.objectSelected != "") {
			gom_config.objectInHand = gom_config.objectSelected;
			gom_config.isHoldingItem = true;
		}
	}
	*/

	return true;
}